home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / perl5 / Debian / AdduserCommon.pm next >
Text File  |  2008-06-26  |  6KB  |  227 lines

  1. use vars qw(@EXPORT $VAR1);
  2.  
  3.  
  4. # Common functions that are used in adduser and deluser
  5. # Copyright (C) 2000 Roland Bauerschmidt <rb@debian.org>
  6.  
  7. # Most of the functions are adopted from the original adduser
  8. # Copyright (C) 1997, 1998, 1999 Guy Maor <maor@debian.org>
  9. # Copyright (C) 1995 Ted Hajek <tedhajek@boombox.micro.umn.edu>
  10. #                     Ian A. Murdock <imurdock@gnu.ai.mit.edu>
  11. #
  12.  
  13. @EXPORT = qw(invalidate_nscd gtx dief warnf read_config get_users_groups get_group_members s_print s_printf systemcall);
  14.  
  15. sub invalidate_nscd {
  16.     # Check if we need to do make -C /var/yp for NIS
  17.     my $nisconfig;
  18.     if(-f "/etc/default/nis") {
  19.         $nisconfig = "/etc/default/nis";
  20.     } elsif(-f "/etc/init.d/nis") {
  21.         $nisconfig = "/etc/init.d/nis";
  22.     }
  23.     # find out whether a local ypserv is running
  24.     # We can ditch any rpcinfo error since if the portmapper is nonfunctional,
  25.     # we couldn't connect to ypserv anyway. If this assumption is invalid,
  26.     # please file a bug and suggest a better way.
  27.     if(defined($nisconfig) && -f "/var/yp/Makefile" &&
  28.         -x "/usr/bin/rpcinfo" && grep(/ypserv/, qx{/usr/bin/rpcinfo -p 2>/dev/null})) {
  29.     open(NISCONFIG, "<$nisconfig");
  30.     if(grep(/^NISSERVER=master/, <NISCONFIG>)) {
  31.             system("make", "-C", "/var/yp");
  32.     }
  33.     close(NISCONFIG);
  34.     }
  35.  
  36.     # Check if we need to invalidate the NSCD cache
  37.     my $nscd = &which('nscd',1);
  38.     # this function replaces startnscd and stopnscd (closes: #54726)
  39.     # We are ignoring any error messages given by nscd here since we
  40.     # cannot expect the nscd maintainer and upstream to document their
  41.     # interfaces. See #330929.
  42.     if(defined($nscd) && -x $nscd)
  43.       {
  44.         my $table = shift;
  45.         if ($table)
  46.           {
  47.             system ($nscd, "-i", $table);
  48.           }
  49.         else
  50.           {
  51.             # otherwise we invalidate passwd and group table
  52.             system ($nscd, "-i", "passwd");
  53.             system ($nscd, "-i", "group");
  54.           }
  55.       }
  56. }
  57.  
  58. sub gtx {
  59.     return gettext( shift );
  60. }
  61.  
  62. sub dief {
  63.     my ($form,@argu)=@_;
  64.     printf STDERR sprintf(gtx("%s: %s"), $0, $form), @argu;
  65.     exit 1;
  66. }
  67.  
  68. sub warnf {
  69.     my ($form,@argu)=@_;
  70.     printf STDERR sprintf(gtx("%s: %s"), $0, $form), @argu;
  71. }
  72.  
  73. # parse the configuration file
  74. # parameters:
  75. #  -- filename of the configuration file
  76. #  -- a hash for the configuration data
  77. sub read_config {
  78.     my ($conf_file, $configref) = @_;
  79.     my ($var, $lcvar, $val);
  80.  
  81.     if (! -f $conf_file) {
  82.     warnf gtx("`%s' does not exist. Using defaults.\n"),$conf_file if $verbose;
  83.     return;
  84.     }
  85.  
  86.     open (CONF, $conf_file) || dief ("%s: `%s'\n",$conf_file,$!);
  87.     while (<CONF>) {
  88.     chomp;
  89.     next if /^#/ || /^\s*$/;
  90.  
  91.     if ((($var, $val) = /^\s*([_a-zA-Z0-9]+)\s*=\s*(.*)/) != 2) {
  92.         warnf gtx("Couldn't parse `%s', line %d.\n"),$conf_file,$.;
  93.         next;
  94.     }
  95.     $lcvar = lc $var;
  96.     if (!defined($configref->{$lcvar})) {
  97.         warnf gtx("Unknown variable `%s' at `%s', line %d.\n"),$var,$conf_file,$.;
  98.         next;
  99.     }
  100.  
  101.     $val =~ s/^"(.*)"$/$1/;
  102.     $val =~ s/^'(.*)'$/$1/;
  103.  
  104.     $configref->{$lcvar} = $val;
  105.     }
  106.  
  107.     close CONF || die "$!";
  108. }
  109.  
  110. # return a user's groups
  111. sub get_users_groups {
  112.     my($user) = @_;
  113.     my($name,$members,@groups);
  114.     setgrent;
  115.     while (($name,$members) = (getgrent)[0,3]) {
  116.     for (split(/ /, $members)) {
  117.         if ($user eq $_) {
  118.         push @groups, $name;
  119.         last;
  120.         }
  121.     }
  122.     }
  123.     endgrent;
  124.     @groups;
  125. }
  126.  
  127. # return a group's members
  128. sub get_group_members
  129.   {
  130.       my $group = shift;
  131.       my @members;
  132.       foreach (split(/ /, (getgrnam($group))[3])) {
  133.       if (getpwuid(getpwnam($_)) eq $_ ) {
  134.           push @members, $_;
  135.       }
  136.       }
  137.       return @members;
  138.   }
  139.  
  140. sub s_print
  141. {
  142.     print join(" ",@_)
  143.     if($verbose);
  144. }
  145.  
  146. sub s_printf
  147. {
  148.     printf @_
  149.     if($verbose);
  150. }
  151.  
  152. sub d_printf
  153. {
  154.     printf @_
  155.         if((defined($verbose) && $verbose > 1) || (defined($debugging) && $debugging == 1));
  156. }
  157.  
  158. sub systemcall {
  159.     my $c = join(' ', @_);
  160.     print ("$c\n") if $verbose==2;
  161.     if (system(@_)) {
  162.     dief (gtx("`%s' returned error code %d. Exiting.\n"), $c, $?>>8)
  163.       if ($?>>8);
  164.         dief (gtx("`%s' exited from signal %d. Exiting.\n"), $c, $?&255);
  165.     }
  166. }
  167.  
  168. sub which {
  169.     my ($progname, $nonfatal) = @_ ;
  170.     for my $dir (split /:/, $ENV{"PATH"}) {
  171.         if (-x "$dir/$progname" ) {
  172.             return "$dir/$progname";
  173.         }
  174.     }
  175.     dief(gtx("Could not find program named `%s' in \$PATH.\n"), $progname) unless ($nonfatal);
  176.     return 0;
  177. }
  178.  
  179.  
  180. # preseed the configuration variables 
  181. # then read the config file /etc/adduser and overwrite the data hardcoded here
  182. sub preseed_config {
  183.   my ($conflistref, $configref) = @_;
  184.   $configref->{"system"} = 0;
  185.   $configref->{"only_if_empty"} = 0;
  186.   $configref->{"remove_home"} = 0;
  187.   $configref->{"home"} = "";
  188.   $configref->{"remove_all_files"} = 0;
  189.   $configref->{"backup"} = 0;
  190.   $configref->{"backup_to"} = ".";
  191.   $configref->{"dshell"} = "/bin/bash";
  192.   $configref->{"first_system_uid"} = 100;
  193.   $configref->{"last_system_uid"} = 999;
  194.   $configref->{"first_uid"} = 1000;
  195.   $configref->{"last_uid"} = 29999;
  196.   $configref->{"first_system_gid"} = 100;
  197.   $configref->{"last_system_gid"} = 999;
  198.   $configref->{"first_gid"} = 1000;
  199.   $configref->{"last_gid"} = 29999;
  200.   $configref->{"dhome"} = "/home";
  201.   $configref->{"skel"} = "/etc/skel";
  202.   $configref->{"usergroups"} = "yes";
  203.   $configref->{"users_gid"} = "100";
  204.   $configref->{"grouphomes"} = "no";
  205.   $configref->{"letterhomes"} = "no";
  206.   $configref->{"quotauser"} = "";
  207.   $configref->{"dir_mode"} = "0755";
  208.   $configref->{"setgid_home"} = "no";
  209.   $configref->{"no_del_paths"} = "^/$ ^/lost+found/.* ^/media/.* ^/mnt/.* ^/etc/.* ^/bin/.* ^/boot/.* ^/dev/.* ^/lib/.* ^/proc/.* ^/root/.* ^/sbin/.* ^/tmp/.* ^/sys/.* ^/srv/.* ^/opt/.* ^/initrd/.* ^/usr/.* ^/var/.*";
  210.   $configref->{"name_regex"} = "^[a-z][-a-z0-9]*\$";
  211.   $configref->{"name_regex_system"} = "^[A-Za-z][-A-Za-z0-9]*\$";
  212.   $configref->{"exclude_fstypes"} = "(proc|sysfs|usbfs|devpts|tmpfs)";
  213.   $configref->{"skel_ignore_regex"} = "dpkg-(old|new|dist)\$";
  214.   $configref->{"extra_groups"} = "dialout cdrom floppy audio video plugdev users games";
  215.   $configref->{"add_extra_groups"} = 0;
  216.  
  217.   foreach( @$conflistref ) {
  218.       read_config($_,$configref);
  219.   }
  220. }
  221.  
  222. # Local Variables:
  223. # mode:cperl
  224. # End:
  225.  
  226. #vim:set ai et sts=4 sw=4 tw=0:
  227.